<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\OrderMessage;
use App\Admin\Repositories\Message;
use App\Handlers\DeviceTypeHandlers;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MessageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Message(), function (Grid $grid) {
            $grid->model()->orderBy('created_at','desc');


            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('content');
            $grid->column('ip');
            $grid->column('user_agent','載具')->width(100)->display(function($column){
                if($column){
                    $user_agent = $column;
                    $device = DeviceTypeHandlers::getDevice($user_agent);
                    return '<style>.p-user-agent{margin: 0}</style><p class="p-user-agent"><span class="order_device">'.$device.'</span><span class="user_agent" style="display: none">'.$user_agent.'</span></p><p class="p-user-agent"><a class="xiangxi" data-action="1" href="javascript:;">詳細</a></p>';
                }else{
                    return "";
                }

            });
            Admin::js(asset('static/admin/js/zorder.js'));
            $grid->column('','關聯訂單')->display(function(){
                return '查看';
            })->modal('關聯訂單',OrderMessage::make());



            $grid->column('created_at')->sortable();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Message(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('email');
            $show->field('content');
            $show->field('ip');
            $show->field('user_agent');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Message(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('phone');
            $form->text('email');
            $form->text('content');
            $form->text('ip');
            $form->text('user_agent');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
